package com.zecan.blog.service.impl;

import com.zecan.blog.common.status.BlogCommonStatus;
import com.zecan.blog.controller.exception.BlogException;
import com.zecan.blog.dao.MessageMapper;
import com.zecan.blog.dao.converter.BlogMessageConverter;
import com.zecan.blog.dto.message.MessageDto;
import com.zecan.blog.entity.Message;
import com.zecan.blog.service.MessageService;
import com.zecan.blog.utils.StringUtils;
import com.zecan.blog.vo.message.MessageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MessageServiceImpl
 * @Description
 * @date 2022/11/22 21:52
 * @Version 1.0
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageMapper messageMapper;


    @Override
    public void addMessage(MessageDto messageDto) {
            // convert 转换实体类
        Message message = BlogMessageConverter.INSTANCE.blogMessageConverter(messageDto);
        // 数据初始化
        message.setMessageCreateTime(StringUtils.dateFormat(new Date()));

        Integer integer = messageMapper.insertMessage(message);

        if(integer != 1) {
            throw new BlogException(BlogCommonStatus.REQUEST_FAIL);
        }
    }

    @Override
    public List<MessageVo> getMessageList() {

        List<Message> allMessageList = messageMapper.getAllMessageList();
        List<MessageVo> messageVoList = new ArrayList<>();
        allMessageList.forEach(message -> {
            messageVoList.add(MessageVo.buildMessageVo(message));
        });
        return messageVoList;
    }

    @Override
    public List<MessageVo> getMessageByLimit(Integer offset, Integer limit) {
        List<Message> allMessageList = messageMapper.getMessageListByLimit(offset, limit);
        List<MessageVo> messageVoList = new ArrayList<>();
        allMessageList.forEach(message -> {
            messageVoList.add(MessageVo.buildMessageVo(message));
        });
        return messageVoList;
    }

    @Override
    public void delMessage(Integer messageId) {
        messageMapper.updateMessageStatus(messageId);
    }
}
